﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.MobileControls;
using System.Web.UI.WebControls;
using Omes.Model;
using Omes.Services;
using Page = Omes.Model.Page;

namespace FashionMural.Admin
{
    public partial class ContentMangeAddChildMenu : System.Web.UI.Page
    {
        protected Omes.Model.Page CurrentPage { get; set; }

        protected List<Omes.Model.Page> CurrentPageChilds { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            DbServer bd = new DbServer();
            List<Omes.Model.Page> pages = bd.GetAllPages();
            try
            {
                int nodeid = Convert.ToInt32(Request.QueryString["nodeid"]);
                if (nodeid <= 0)
                {
                    CurrentPage = pages[1];
                    CurrentPageChilds = pages.Where(p => (p.ParentId == CurrentPage.Id)).ToList();
                }
                else
                {
                    CurrentPage = pages.Where(p => (p.Id == nodeid)).FirstOrDefault();
                    CurrentPageChilds = pages.Where(p => (p.ParentId == nodeid)).ToList();
                }
                //this.ShowBarMenu();
            }
            catch (Exception)
            {
                CurrentPage = pages[1];
                CurrentPageChilds = pages.Where(p => (p.ParentId == CurrentPage.Id)).ToList();
            }
            if(!Page.IsPostBack)
            {

            }
        }

        //protected void ShowBarMenu()
        //{
        //    Omes.Services.DbServer db = new DbServer();
        //    List<Omes.Model.Page> pages = db.GetAllPages();
        //    // get first node page
        //    List<Omes.Model.Page> firstpages = pages.Where(p => (p.ParentId == 1)).ToList();
        //    // add to treeview control
        //    foreach (Page firstpage in firstpages)
        //    {
        //        Omes.Model.Page temp = firstpage;
        //        TreeNode fristnode = new TreeNode(temp.PageName, temp.Id.ToString(), "",
        //                                          "/ContentMangeAddChildMenu.aspx?nodeid="+temp.Id, "");
        //        //get fristnode childnode
        //        List<Omes.Model.Page> childpages = pages.Where(p => (p.ParentId == temp.Id)).ToList();
        //        //add to first node
        //        foreach (Page childpage in childpages)
        //        {
        //            fristnode.ChildNodes.Add(new TreeNode(childpage.PageName, childpage.Id.ToString(), "","/ContentManageChildMenuEdit.aspx?nodeid="+childpage.Id, ""));
        //        }
        //        BarMenu.Nodes.Add(fristnode);
        //        BarMenu.CollapseAll();
        //        BarMenu.ShowLines = true;
        //        BarMenu.NodeStyle.NodeSpacing = 10;
        //        BarMenu.NodeStyle.HorizontalPadding = 5;
        //        BarMenu.SelectedNodeStyle.BackColor = System.Drawing.Color.DarkGray;
        //    }
        //}

        public List<Omes.Model.PageType> GetPageTypes(int id)
        {
            List<Omes.Model.PageType> result = new List<PageType>();
            DbServer bd = new DbServer();
            result = bd.GetPageTypes(id);
            return result;
        }

        public Omes.Model.Page GetPage(int id)
        {
            Omes.Model.Page result = new Page();
            DbServer db = new DbServer();
            result = db.GetPage(id);
            if (result == null)
            {
                result = new Page(){PageName = ""};
            }
            return result;
        }

        public Omes.Model.PageType GetPageType(int id)
        {
            PageType type = new PageType();
            DbServer db = new DbServer();
            type = db.GetPageType(id);
            return type;
        }
    }
}